// EXE_SqlSlicedInserter.cpp : Defines the entry point for the application.
// Rewrite as a dialog app

#include "stdafx.h"
#include "EXE_SqlSlicedInserter.h"
#include "EXE_SqlSlicedInserterThread.h"

// EXE_SqlSlicedInserter.cpp : Defines the entry point for the console application.
// DLL_FileSourceReader_Template
// DLL_SqlInserter_Template

#ifndef gbr_EXE_SqlSlicedInserter_Dialog
#define gbr_EXE_SqlSlicedInserter_Dialog

INT_PTR CALLBACK MainDialog(HWND, UINT, WPARAM, LPARAM);
PThreadData pElThreadData = NULL;
HINSTANCE hInstance = NULL;

//int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int)
int APIENTRY wWinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int)
{
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);

	hInstance = hInst;
	pElThreadData = StartElThread(::GetCommandLine());
	DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), NULL, MainDialog);
	return 0;
}

INT_PTR MainDialogCommand(HWND hDlg, WPARAM wParam, LPARAM lParam)
{
	UNREFERENCED_PARAMETER(lParam);
	INT_PTR pRes = (INT_PTR)TRUE;
	switch (LOWORD(wParam))
	{
	case IDOK: // [Ok]
		EndDialog(hDlg, LOWORD(wParam));
		break;
	case IDSTOP: // [Cancel]
	case IDCANCEL: // Alt-F4
	case IDM_EXIT: // File -> Exit
		StopElThread(&pElThreadData);
		EndDialog(hDlg, LOWORD(wParam));
		break;
	default:
		pRes = (INT_PTR)FALSE;
	}

	return pRes;
}

void MapControlsToThread(HWND hDlg)
{
	::InitializeCriticalSection(&(pElThreadData->c.cs));
	::EnterCriticalSection(&(pElThreadData->c.cs));

	pElThreadData->c.hDlg = hDlg;
	pElThreadData->c.hOk = GetDlgItem(hDlg, IDOK);
	pElThreadData->c.hCancel = GetDlgItem(hDlg, IDSTOP);
	pElThreadData->c.hProgress = GetDlgItem(hDlg, IDC_PROGRESS1);
	SendMessage(pElThreadData->c.hProgress, PBM_SETRANGE, 0, MAKELPARAM(0, 10000));
	for (int i = 0; i < 10; i++)
	{
		pElThreadData->c.hProgressBars[i] = GetDlgItem(hDlg, IDC_PROGRESS2 + i);
		SendMessage(pElThreadData->c.hProgressBars[i], PBM_SETRANGE, 0, MAKELPARAM(0, 1000));
	}
	pElThreadData->c.hStatusText = GetDlgItem(hDlg, IDC_STATICSTATUS);
	pElThreadData->c.bActive = true;

	::LeaveCriticalSection(&(pElThreadData->c.cs));
}

INT_PTR CALLBACK MainDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	UNREFERENCED_PARAMETER(lParam);
	INT_PTR pRes = (INT_PTR)TRUE;

	switch (message)
	{
	case WM_INITDIALOG: // TODO: Map thread objects to gui
		{
			INITCOMMONCONTROLSEX initCtr = {sizeof INITCOMMONCONTROLSEX, ICC_PROGRESS_CLASS};
			InitCommonControlsEx(&initCtr);
		}
		::LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_EXE_SQLSLICEDINSERTER));
		
		MapControlsToThread(hDlg);
		break;
	case WM_DESTROY:
		StopElThread(&pElThreadData);
		PostQuitMessage(0);
		break;
	case WM_COMMAND:
		pRes = MainDialogCommand(hDlg, wParam, lParam);
		break;
	default:
		pRes = (INT_PTR)FALSE;
	}
	return pRes;
}

#endif // gbr_EXE_SqlSlicedInserter_Dialog